package com.black.knight.chess.providers;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.black.knight.chess.domain.PNGGame.PNGGames;
import com.black.knight.chess.domain.PNGOpening.PNGOpenings;
import com.black.knight.chess.domain.PNGProblem.PNGProblems;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "blackknightchess.db";
    private static final int DATABASE_VERSION = 14;
	
	DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + PNGOpeningsContentProvider.PNG_OPENINGS_TABLE_NAME + " (" 
        		+ PNGOpenings.PNG_GAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
        		+ PNGOpenings.DESCRIPTION + " VARCHAR,"
        		+ PNGOpenings.FEN + " VARCHAR,"
        		+ PNGOpenings.PNG + " VARCHAR"
                + ");");
        
        db.execSQL("CREATE TABLE " + PNGGamesContentProvider.PNG_GAMES_TABLE_NAME + " (" 
        		+ PNGGames.PNG_GAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
        		+ PNGGames.DESCRIPTION + " VARCHAR,"
        		+ PNGGames.CATEGORY + " VARCHAR,"
        		+ PNGGames.FEN + " VARCHAR,"
        		+ PNGGames.PNG + " VARCHAR"
                + ");");
        
        db.execSQL("CREATE TABLE " + PNGProblemsContentProvider.PNG_PROBLEMS_TABLE_NAME + " (" 
        		+ PNGProblems.PNG_PROBLEM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
        		+ PNGProblems.DESCRIPTION + " VARCHAR,"
        		+ PNGProblems.MATE_IN + " INTEGER,"
        		+ PNGProblems.FEN + " VARCHAR,"
        		+ PNGProblems.PNG + " VARCHAR"
                + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + PNGOpeningsContentProvider.PNG_OPENINGS_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + PNGGamesContentProvider.PNG_GAMES_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + PNGProblemsContentProvider.PNG_PROBLEMS_TABLE_NAME);
        onCreate(db);
    }
}